package com.ljc.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ljc.dto.Result;
import com.ljc.entity.PurchaseApply;
import com.ljc.entity.PurchaseApplyReview;
import com.ljc.service.PurchaseApplyService;
import com.ljc.util.JWTUtil;
import com.ljc.vo.PurchaseApplySearByUser;
import com.ljc.vo.PurchaseApplySearchByAdmit;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.view.RedirectView;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
@ResponseBody
@RequestMapping("purchaseApply")
@CrossOrigin
@Slf4j
public class PurchaseApplyController {
    @Autowired
    private PurchaseApplyService purchaseApplyService;

    @RequestMapping("insert")
    public Result insert(HttpServletRequest request, @RequestBody PurchaseApply purchaseApply) {
        Result result = new Result();
        log.info("[{}]",purchaseApply);
        String token = request.getHeader("accessToken");
        String id = JWTUtil.getTokenInfo(token).getClaim("id").asString();
        purchaseApply.setUserId(Integer.valueOf(id));
        try {
            purchaseApplyService.insert(purchaseApply);
            result.setStatus(1).setMsg("插入购置申请成功");
        }catch (Exception e){
            e.printStackTrace();
            result.setStatus(1).setMsg("失败");
        }
        return result;
    }
    @RequestMapping("searchByUser")
    public Result searchByUser(HttpServletRequest request,@RequestBody PurchaseApplySearByUser search){
        Result result = new Result();
        String token = request.getHeader("accessToken");
        String id = JWTUtil.getTokenInfo(token).getClaim("id").asString();
        search.setUserId(Integer.valueOf(id));
        log.info("[{}]",search);
        PageHelper.startPage(search.getPageNum(),search.getPageSize());
        List<PurchaseApply> purchaseApplies = purchaseApplyService.searchByUser(search);
        PageInfo<PurchaseApply> purchaseApplyPageInfo = new PageInfo<>(purchaseApplies);
        result.setStatus(1).setMsg("查找用户的购置申请成功").setData(purchaseApplyPageInfo);
        return result;
    }
    @RequestMapping("findById")
    public Result findById(@RequestParam int id){
        Result result = new Result();
        PurchaseApply one = purchaseApplyService.findById(id);
        result.setStatus(1).setMsg("获取设备购置申请信息成功").setData(one);
        return result;
    }
    @RequestMapping("findByAdmit")
    public Result findByAdmit(HttpServletRequest request, @RequestBody PurchaseApplySearchByAdmit search){
        Result result = new Result();
        String token = request.getHeader("accessToken");
        String id = JWTUtil.getTokenInfo(token).getClaim("id").asString();
        search.setAdmitId(Integer.valueOf(id));
        PageHelper.startPage(search.getPageNum(),search.getPageSize());
        List<PurchaseApply> byAdmit = purchaseApplyService.findByAdmit(search);
        PageInfo<PurchaseApply> purchaseApplyPageInfo = new PageInfo<>(byAdmit);
        result.setStatus(1).setMsg("根据管理员查找购置申请成功").setData(purchaseApplyPageInfo);
        return result;
    }
    @RequestMapping("review")
    public Result review(@RequestBody   PurchaseApplyReview review){
        Result result = new Result();
        try {
            purchaseApplyService.review(review);
            result.setStatus(1).setMsg("审核成功");
        }catch (Exception e){
            e.printStackTrace();
            result.setMsg("失败").setStatus(0);
        }
        return result;
    }
    @RequestMapping("/getReview")
    public Result getReview(@RequestParam int id){
        Result result = new Result();
        PurchaseApplyReview review = purchaseApplyService.getReview(id);
        result.setStatus(1).setMsg("获取审核结果成功").setData(review);
        return  result;
    }
    @RequestMapping("countByUserId")
    public Result countByUserId(HttpServletRequest request){
        Result result = new Result();
        String token = request.getHeader("accessToken");
        String id = JWTUtil.getTokenInfo(token).getClaim("id").asString();
        int i = purchaseApplyService.countByUserId(Integer.valueOf(id));
        result.setMsg("获取总计划数成功").setStatus(1).setData(i);
        return  result;
    }
}
